package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.common.util.ExcelUtil;
import com.recharge.common.util.TemplateUtils;
import com.recharge.domain.query.WithdrawQueryDO;
import com.recharge.domain.vo.WithdrawDO;
import com.recharge.service.WithdrawService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 提现接口 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/withdraw")
public class WithdrawController extends BaseController {

    @Value("${file.withdraw.template}")
    private String fileTemplatePath;

    @Autowired
    WithdrawService withdrawService;

    @RequiresPermissions("withdraw:page")
    @PostMapping(value = "/page.html")
    @ApiOperation(value = "查询提现记录")
    public @ResponseBody
    ObjectResponse get(@RequestBody WithdrawQueryDO domain) {
        ObjectResponse response = new ObjectResponse();

        ResultDO result = withdrawService.find(domain);
        if (result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("withdraw:apply")
    @PostMapping(value = "/apply.html")
    @ApiOperation(value = "申请提现")
    public @ResponseBody
    ObjectResponse apply(@RequestBody WithdrawDO domain) {
        ObjectResponse response = new ObjectResponse();
        if (!domain.verification()) {
            response.setMsg(ErrorCode.PARAM_VERIFICATION_ERROR.getMsg());
            response.setStatus(ErrorCode.PARAM_VERIFICATION_ERROR.getCode());
            return response;
        }

        ResultDO result = withdrawService.applyWithdraw(domain, getUserName());
        if (result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("withdraw:verify")
    @PostMapping(value = "/verify.html")
    @ApiOperation(value = "审核提现")
    public @ResponseBody
    ObjectResponse verify(@RequestBody WithdrawDO domain) {

        ObjectResponse response = new ObjectResponse();
        /*
            参数校验
         */
        if (domain.getId() == null && domain.getStatus() == null) {
            response.setMsg(ErrorCode.PARAM_GET_ERROR.getMsg());
            response.setStatus(ErrorCode.PARAM_GET_ERROR.getCode());
            return response;
        }
//        if (domain.getStatus() == Constant.WITHDRAW_VERIFICATION_SUCCESS) {
//            if (domain.getSerialNumber() == null) {
//                response.setMsg(ErrorCode.PARAM_GET_ERROR.getMsg());
//                response.setStatus(ErrorCode.PARAM_GET_ERROR.getCode());
//                return response;
//            }
//        }

        ResultDO result = withdrawService.verifyWithdraw(domain, getUserName());
        if (result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("withdraw:excel-download")
    @RequestMapping(value = "/excelDownload.html", method = RequestMethod.POST)
    @ApiOperation(value = "excel文件导出提现记录", notes = "")
    public void excelDownload(@RequestBody WithdrawQueryDO query, HttpServletResponse response) {
        String[] statuses = {"待审核", "审核通过", "审核驳回"};
        ResultDO result = withdrawService.find(query);
        PageDO<WithdrawDO> lists = (PageDO<WithdrawDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        List<WithdrawDO> list = lists.getData();
        List<Object> downloadList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> data = new HashMap<>();
            data.put("a1", item.getWithdrawNo());
            data.put("a2", item.getMerchantName());
            data.put("a3", item.getAmount() / 1000);
            data.put("a4", item.getBankName());
            data.put("a5", item.getBankAccount());
            data.put("a6", item.getApplicant());
            data.put("a7", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getApplicantDate()));
            data.put("a8", item.getChecker());
            data.put("a9", statuses[item.getStatus()]);
            data.put("a10", (!TemplateUtils.isEmpty(item.getBeforeBalance())) ? item.getBeforeBalance() / 1000 : "");
            data.put("a11", (!TemplateUtils.isEmpty(item.getAfterBalance())) ? item.getAfterBalance() / 1000 : "");
            downloadList.add(data);
        });
        ExcelUtil.exportExcel(response, downloadList, fileTemplatePath, "提现记录列表.xls");
    }

}
